
#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
#include "lv_user_sqlist.h"

/**
 * @Function: 
 * @Descripttion: 删除第index个元素
 * @param {SqlistType_t} *list
 * @param {uint8_t} index
 * @return {*}
 */
StatusType lv_user_list_delete(SqlistType_t *list,uint8_t index)
{
    
    uint8_t length = list->length;
    if((index < 0) || (index > length))
        return OVERFLOW;
    //-----------------------------------
    ElemType_t *delete_ptr = &list->data[index];
    //尾元素地址+1
    ElemType_t *p = &list->data[length];
    for(;delete_ptr < p;delete_ptr++)
    {
        *delete_ptr = *(delete_ptr+1);
    }   
    list->length --;
    return SUCCESS;
}

/**
 * @Function: 
 * @Descripttion: 在第index个元素之前插入新元素
 * @param {SqlistType_t} *list
 * @param {uint8_t} index
 * @param {ElemType_t} e
 * @return {*}
 */
StatusType lv_user_list_insert(SqlistType_t *list,uint8_t index,ElemType_t e)
{
    uint8_t length = list->length;
    if((index < 0) || (index > length))
        return OVERFLOW;  
    //-----------------------------------
    ElemType_t *insert_ptr = &(list->data[index]);
    //尾元素地址+1
    ElemType_t *ptr = &(list->data[length]);
    for(;insert_ptr < ptr;--ptr)
    {
        *ptr = *(ptr-1);
    }
    *insert_ptr = e;
    list->length ++;
    return SUCCESS;
}
/**
 * @Function: 
 * @Descripttion: 替换第i元素
 * @param {SqlistType_t} *list
 * @param {uint8_t} index
 * @param {ElemType_t} e
 * @return {*}
 */
StatusType lv_user_list_replace(SqlistType_t *list,uint8_t index,ElemType_t e)
{
    uint8_t length = list->length;
    if((index < 0) || (index > length))
	    return OVERFLOW;  
	list->data[index] = e;
    return SUCCESS;
}

/**
 * @Function: 
 * @Descripttion: 根据元素获取id
 * @param {ElemType_t} e
 * @return {*} note:0xff->fail
 */
uint8_t lv_user_list_get_elem_id(SqlistType_t *list,ElemType_t e)
{
    uint8_t i = 0;
    ElemType_t *p = list->data;
    uint8_t length = list->length;
    while ((i < length) && (*p++ != e))
    {
        ++i;
    }
    if(i < length)
        return i;
    else
        return 0xff;   
}

/**
 * @Function: 
 * @Descripttion: 获取sqlist长度
 * @param {SqlistType_t} *list
 * @return {*}
 */
uint8_t lv_user_list_get_sqlist_length(SqlistType_t *list)
{
    return list->length;
}

/**
 * @Function: 
 * @Descripttion: 获取第index个元素的上一元素
 * @param {SqlistType_t} *list
 * @param {uint8_t} index
 * @return {*}
 */
ElemType_t lv_user_list_get_prev_elem(SqlistType_t *list,uint8_t index)
{
    uint8_t length = list->length;
    if(index == 0)
        index = length-1;
    else
        index --;
    return list->data[index];
}
/**
 * @Function: 
 * @Descripttion: 获取第index个元素的下一元素
 * @param {SqlistType_t} *list
 * @param {uint8_t} index
 * @return {*}
 */
ElemType_t lv_user_list_get_next_elem(SqlistType_t *list,uint8_t index)
{
    uint8_t length = list->length;
    index ++;
    index %= length;
    return list->data[index];
}

/**
 * @Function: 
 * @Descripttion: 获取第index个元素
 * @param {SqlistType_t} *list
 * @param {uint8_t} index
 * @return {*}
 */
ElemType_t lv_user_list_get_cur_elem(SqlistType_t *list,uint8_t index)
{
    return list->data[index];
}
/**
 * @Function: 
 * @Descripttion: 遍历整个list元素
 * @param {SqlistType_t} *list
 * @return {*}
 */
void ListTraverse(SqlistType_t *list)
{
    uint8_t length = list->length;
    printf("length:%d,sqlist all data:\r\n",length);
    for (uint8_t i = 0; i < length; i++)
    {
        printf("0x%x,",list->data[i]);
    }
    printf("\r\n");
}

